<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">

<head>
    <th:block th:include="common/common_head :: commonHeader('采购申请头文件')"/>
    <link rel="stylesheet" th:href="@{/static/js/plugins/jquery-layout/jquery.layout-latest.css}">
    <!-- <style type="text/css">.table {table-layout:fixed;}</style> -->
</head>

<body class="gray-bg">

<div class="container-div ui-layout-center">
    <div class="row">
        <div class="col-sm-12 search-condition">
            <form id="nproPurchRequestHeaderForm" th:action="@{/sampleNproRequest/list}" class="form-inline">
                <div class="select-list">
                    <ul>
                        <li class="col-sm-3" style="width: 33%">
                            <label>部门：</label>
                            <select id="purchRequestDept" name="purchRequestDept" sysDict="BM" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>成本中心：</label>
                            <select id="costCenter" name="costCenter" sysDict="CBZX" emptyFirst="true"></select>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>采购申请单号：</label>
                            <input type="text" name="purchRequestCode"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <div>
                                <label>申请者：</label>
                                <input type="text" name="purchRequestStaff"/>
                            </div>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>需求日期(起)：</label>
                            <input type="text" name="requestDate" id="requestDate"/>
                        </li>
                        <li class="col-sm-3" style="width: 33%">
                            <label>需求日期(止)：</label>
                            <input type="text" name="deliveryDate" id="deliveryDate"/>
                        </li>

                        <li class="col-sm-3" style="width: 33%">
                            <button id="nproPurchRequestHeaderSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索</button>
                            <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置</button>
                            <button id="nproPurchRequestHeaderAdvancedSearch" type="button" class="btn btn-primary btn-rounded btn-sm" style="display: none"><i class="fa fa-caret-down"></i>&nbsp;高级搜索</button>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="col-sm-12 select-info order-table">
            <div class="btn-group hidden-xs toolbar" id="nproPurchRequestHeaderToolBar" role="group">
                <!-- <a class="btn btn-outline btn-success btn-rounded" onclick="toAddnproPurchRequestHeader()">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="toEditnproPurchRequestHeader()">
                    <i class="fa fa-edit"></i> 编辑
                </a> -->

                <a class="btn btn-outline btn-success btn-rounded" onclick="inquiry()">
                    <i class="fa fa-save"></i> 询价
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="generateQuotations()">
                    <i class="fa fa-save"></i> 生成报价单
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="offer()">
                    <i class="fa fa-save"></i> 报价邮件通知
                </a>
                <a class="btn btn-outline btn-success btn-rounded" onclick="parity()">
                    <i class="fa fa-save"></i> 比价
                </a>
                <!-- <a class="btn btn-outline btn-success btn-rounded" onclick="supplyGood()">
                    <i class="fa fa-save"></i> 货源分配
                </a> -->
                <a class="btn btn-outline btn-success btn-rounded" id="googds" data-toggle='modal' data-target='' onclick="show()">
                    <i class="fa fa-save"></i> 分配货源
                </a>
            </div>
            <div style="height:360px; overflow:auto;">
                <table id="nproPurchRequestHeader" data-mobile-responsive="true"></table>
            </div>
            <div>
                <table id="nproPurchRequestItem" data-mobile-responsive="true"></table>
            </div>
        </div>
    </div>
</div>

<!-- 指定报价的供应商 -->
<div class="modal fade" id="myModalVendor" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document" style="width:70%">
        <div class="modal-content" style="height:700px">
            <form id="inquiryVendorForm" th:action="@{/inquiryQuotation/list?poFlag=PR}" class="form-inline">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">供应商报价信息</h4>
                </div>
                <div class="modal-body" style="height:90%;width:85%;overflow:auto">

                    <div style="height: 30px;width:100%">
                        <ul>
                            <li class="col-sm-3" style="width: 40%">
                                <label  style="width: 100px">供应商编码：</label>
                                <input name="vendorCode" type="text" id="vendorCode" placeholder="供应商编码" class="form-control">
                            </li>
                            <li class="col-sm-3" style="width: 40%">
                                <label  style="width: 120px">供应商名称：</label>
                                <input name="vendorName" type="text" id="vendorName" class="form-control" placeholder="供应商名称">
                            </li>
                            <li class="col-sm-3" style="width: 20%">
                                <button id="inquiryVendorSearchBtn" type="button" class="btn btn-primary btn-rounded btn-sm"><i class="fa fa-search"></i>&nbsp;搜索</button>
                                <button class="btn btn-warning btn-rounded btn-sm" type="reset"><i class="fa fa-eraser"></i>&nbsp;重置</button>
                            </li>
                        </ul>
                    </div>
                    <table class="table table-bordered text-nowrap" id="inquiryVendor">

                    </table>
                </div>
            </form>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick='check_form("inquiryVendor")'>确定</button>
                <!--<button type="button" class="btn btn-primary" onclick='toEditPurchRequestItem()'>确定</button>-->
            </div>
        </div>
    </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->

<!--引入公共js-->
<div th:include="common/onload_js :: onloadJS"></div>
<script type="text/javascript" th:src="@{/static/js/plugins/jquery-layout/jquery.layout-latest.js}"></script>

<!-- Page-Level Scripts -->

<!--  -->
<script type="text/javascript">
    var itemBS = Searcher.createBS('#nproPurchRequestItem',
        [
            {checkbox: true},
            {title: "采购申请编码", field:"purchRequestCode"},
            {title: "采购申请行项目编码",field:"purchRequestItemCode"},
            {title: "货源状态",field:"goodsStatus",
                formatter : function (value, row, index) {
                    if(value ==-2) {
                        return "草稿";
                    } else if(value == -1) {
                        return "采购申请询价中";
                    }else if(value == 0) {
                        return "货源已指定";
                    } else if(value == 1) {
                        return "订单已提交";
                    }else if(value == 2) {
                        return "订单审批中";
                    }else if(value == 3) {
                        return "订单审批通过";
                    }else if(value == -3) {
                        return "订单审批驳回";
                    }else {
                        return value;
                    }
                }
            },
            {title: "科目类别",field:"subjectCategory"},
            {title: "采购组",field:"purchGroup"},
            {title: "采购组织",field:"purchOrg"},
            {title: "使用人",field:"applier"},
            {title: "物料描述",field:"materialDesc"},
            {title: "工厂",field:"factory"},
            {title: "库存地点",field:"storageLocation"},
            {title: "物料组",field:"materialGroup"},
            {title: "申请数量",field:"applyQty"},
            {title: "计量单位",field:"measureUnit"},
            {title: "成本中心",field:"costCenter"},
            {title: "内部订单号",field:"internalOrderCode"},
            {title: "主资产号",field:"mainAssetsCode"},
            {title: "资产次级编号",field:"assetsSencondCode"},
            {title: "总账科目",field:"ledgerAccount"},
            {title: "需求日期",field:"requestDate"},
            {title: "交货日期",field:"deliveryDate"},
            {title: "交货地址",field:"deliveryAddress"},
            {title: "备注",field:"remark"}
        ], {"showColumns": false,"showRefresh": false, "firstLoad": false}
    );

    //面向对象
    var searcherBS= Searcher.createBS('#nproPurchRequestHeader',
        [
            {radio: true},
            {title: "采购申请编码", field:"purchRequestCode"},
            {title: "采购申请类型 ",field:"purchRequestType"},
            {title: "创建人",field:"purchRequestStaff"},
            {title: "申请部门",field:"purchRequestDept"},
            {title: "流程状态",field:"goodsStatus",
                formatter : function (value, row, index) {
                    if(value ==-2) {
                        return "草稿";
                    } else if(value == -1) {
                        return "采购申请询价中";
                    }else if(value == 0) {
                        return "货源已指定";
                    } else if(value == 1) {
                        return "订单已提交";
                    }else if(value == 2) {
                        return "订单审批中";
                    }else if(value == 3) {
                        return "订单审批通过";
                    }else if(value == -3) {
                        return "订单审批驳回";
                    }else {
                        return value;
                    }
                }
            }
        ], {pageList: [5, 10, 15, 50, 100], pageSize : 5,
            onCheck : function(row) {itemBS.getData({'url': ctxPath + '/nproPurchRequestItem/list?purchRequestCode='+row.purchRequestCode});},
            onPostBody : function() {$("#nproPurchRequestItem").bootstrapTable('removeAll');}
        }
    );
    //刷新数据
    function refresh() {
        searcherBS.refresh();
    }

    // 初始化时间插件
    laydate.render({elem: '#requestDate'});
    laydate.render({elem: '#deliveryDate'});

    initDictDataSel();//初始化所有字典数据
    /**
     * 跳转至新增页
     */
    function toAddnproPurchRequestHeader() {
        var index=Dialog.openUrl("新增采购订单", ctxPath + '/modules/requisition/requisition_add.html',{area : ['900px','700px'],
            maxmin: true,
            end: function() {}
        });
        layer.full(index);
    }

    /**
     * 编辑
     */
    function toEditnproPurchRequestHeader() {
        //field:"pkId"
        var rowDatas = searcherBS.getSelectedRows("pkId");
        if (rowDatas.length != 1) {
            Dialog.errorMsg("请选择一行进行编辑");
            return;
        }
        var index = Dialog.openUrl("编辑供应商信息", ctxPath + '/nproPurchRequestHeader/toEdit/'+rowDatas,
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {}
            });
        layer.full(index)
    }

    function deletePurchRequestHeader() {
        var rowDatas = searcherBS.getSelectedRows("pkId");
        if (rowDatas.length < 1) {
            Dialog.errorMsg("请至少选择一行");
            return;
        }
        Dialog.confirm('确定删除？', function(index){
            Page.ajaxPOST(ctxPath + "/nproPurchRequestHeader/delete", {userIds: rowDatas}, function () {
                Dialog.msg("操作成功", function () {
                    searcherBS.refresh();
                });
            });
        });
    }

    /***************2019-06-05 yf*****************************/

    /**** 询价********/
    function inquiry(){
        var rowData = searcherBS.getSelectedRows("pkId");
        var rowDatas = $("#nproPurchRequestHeader").bootstrapTable('getSelections');

        //行项目的id
        var itemrowData = itemBS.getSelectedRows("pkId");
        var itemrowDatas = $("#nproPurchRequestItem").bootstrapTable('getSelections');

        var goodsStatus;
        for(var i=0;i<itemrowDatas.length;i++){
            goodsStatus = itemrowDatas[i].goodsStatus;
        }
        if (rowData.length < 1 && itemrowData.length < 1) {
            Dialog.errorMsg("请至少选择一行进行编辑");
            return;
        }
        if(goodsStatus==-2){
            var index = Dialog.openUrl("新增询价单", ctxPath + '/nproPurchRequestHeader/inquiry/'+rowData+'/'+itemrowData,
                {area : ['900px','700px'],
                    maxmin: true,
                    end: function () {}
                });
            layer.full(index)
        }
        if(goodsStatus==-1){
            Dialog.errorMsg("此采购订单已询价！！！");
        }
        if(goodsStatus==-1){
            Dialog.errorMsg("此采购订单已询价！！！");
        }
        if(goodsStatus==0){
            Dialog.errorMsg("此采购订单已货源分配！！！");
        }
        if(goodsStatus == 1) {
            Dialog.errorMsg("订单已提交！！！");
        }
        if(goodsStatus == 2) {
            Dialog.errorMsg("订单价格审批中！！！");
        }
        if(goodsStatus == 3) {
            Dialog.errorMsg("订单价格审批通过！！！");
        }
        if(goodsStatus == -3) {
            Dialog.errorMsg("订单价格审批驳回！！！");
        }

    }
    //生成报价单
    function generateQuotations(){
        var index = Dialog.openUrl("生成报价单", ctxPath + '/nproPurchRequestHeader/generateQuotations/',
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {}
            });
        layer.full(index)
    }

    /********报价*************/
    function offer(){
        var index = Dialog.openUrl("报价", ctxPath + '/nproPurchRequestHeader/offer/',
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {}
            });
        layer.full(index)
    }
    /***********比价****************/
    function parity(){
        var index = Dialog.openUrl("比价", ctxPath + '/nproPurchRequestHeader/parity/',
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {}
            });
        layer.full(index)
    }
    /************货源分配*****2019-06-05 待修改*************/
    function show(){
        //var rows = $("#nproPurchRequestHeader").bootstrapTable('getSelections');
        var rows = $("#nproPurchRequestItem").bootstrapTable('getSelections');
        //$("input:text").attr("name");      // 获取name属性值
        //$("input:text").attr("name",str);  // 设置name属性值为str
        if(rows.length==1){
            $("#googds").attr("data-target","#myModalVendor");
            for (var i=0;i<rows.length;i++){
                if(rows[i].goodsStatus==-2||rows[i].goodsStatus==3){
                    searcher1 = Searcher.createBS('#inquiryVendor',
                        [
                            {checkbox: true},
                            {title: "供应商编码", field:"vendorCode"},
                            {title: "供应商名称", field:"vendorName"},
                            {title: "含税单价",field:"price"},
                            {title: "价格单位",field:"priceUnit"},
                            {title: "币种",field:"currency"},
                            {title: "税码",field:"taxCode"}
                        ],{pageList: [10], pageSize : 10, showColumns: false, showRefresh: false}
                    );
                }else{
                    $("#googds").attr("data-target","");
                    if(rows[i].goodsStatus==0){
                        return Dialog.alert("货源已经指定 不允许在次货源指定");
                    }
                    if(rows[i].goodsStatus==-1){
                        return Dialog.errorMsg("此采购订单询价中不允许货源指定！！！");
                    }
                    if(rows[i].goodsStatus == 1) {
                        return Dialog.errorMsg("订单价格已提交不允许货源指定！！！");
                    }
                    if(rows[i].goodsStatus == 2) {
                        return Dialog.errorMsg("订单价格审批中不允许货源指定！！！");
                    }
                    if(rows[i].goodsStatus == -3) {
                        return Dialog.errorMsg("订单价格审批驳回不允许货源指定！！！");
                    }

                }
            }
        }else{
            $("#googds").attr("data-target","");
            Dialog.alert("请先选中一行进行货源分配！")
        }
    }
    function check_form(obj){
        //使用getSelections即可获得，row是json格式的数据
        var rows = $("#"+obj).bootstrapTable('getSelections');
        var rowDatas=new Array();
        //遍历选中的值
        for (var i=0;i<rows.length;i++){
            rowDatas.push(rows[i].quotationId);
        }
        //判断是否选中行
        if (!rows || rows.length == 0) {
            Dialog.alert("请先选中一行！")
            return;
        }
        /*******修改*************/
        //遍历选中的值
        showTableData(obj);
        toEditPurchRequestItem(rowDatas);
    }
    function showTableData(obj) {
        if("inquiryVendor"==obj){
            $('#myModalVendor').modal('hide');
        }
        if("ordel"==obj){
            $('#myModalGoods').modal('hide');
        }
    }
    /**
     * 编辑多条信息
     */
    function toEditPurchRequestItem(rowDatas1) {

        //非生产性物料
        var rowDatas = itemBS.getSelectedRows("pkId");
        //purchRequestItemCode
        if (rowDatas.length < 1) {
            Dialog.errorMsg("请至少选择一行进行编辑");
            return;
        }
        // itemBS.getData({'url': ctxPath + '/nproPurchRequestItem/list?purchRequestCode='+row.purchRequestCode});
        var index = Dialog.openUrl("货源分配信息", ctxPath + '/nproPurchRequestItem/toAllot/?ids='+rowDatas+'&inqids='+rowDatas1,
            {area : ['900px','700px'],
                maxmin: true,
                end: function () {}
            });
        layer.full(index)

    }
</script>
</body>
</html>